package org.esprit.ocm.client.model.db.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.db.ServerRecord;
import org.esprit.ocm.dto.impl.ServerDto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ServerLocalDS extends DataSource {

	private static ServerLocalDS	instance	= null;

	public static ServerLocalDS getInstance(List<ServerDto> list) {
//		if (instance == null) {
			instance = new ServerLocalDS("serverLocalDS", list);
//		}
		return instance;
	}

	public ServerLocalDS(String id, List<ServerDto> list) {

//		setID(id);

		// DataSourceTextField idServerField = new
		// DataSourceTextField("idServer", "idServer");
		DataSourceTextField nameServerField = new DataSourceTextField("nameServer", "nameServer");
		DataSourceTextField ipServerField = new DataSourceTextField("ipServer", "ipServer");
		DataSourceTextField typeServerField = new DataSourceTextField("typeServer", "typeServer");
		// DataSourceTextField portField = new DataSourceTextField("port",
		// "port");
		// DataSourceTextField useHTTPSField = new
		// DataSourceTextField("useHTTPS", "useHTTPS");
		// DataSourceTextField resourcePrefixField = new
		// DataSourceTextField("resourcePrefix", "resourcePrefix");
		// DataSourceTextField keyFingerprintField = new
		// DataSourceTextField("keyFingerprint", "keyFingerprint");
		// DataSourceTextField keyMaterialField = new
		// DataSourceTextField("keyMaterial", "keyMaterial");

		// setFields(idServerField, nameServerField, ipServerField, portField,
		// useHTTPSField, resourcePrefixField, keyFingerprintField,
		// keyMaterialField);
		setFields(nameServerField, ipServerField, typeServerField);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (ServerDto from : list) {

			ServerRecord to = new ServerRecord();

			to.setIdServer(String.valueOf(from.getIdServer()));
			to.setNameServer(from.getNameServer());
			to.setIpServer(from.getIpServer());
			to.setPort(String.valueOf(from.getPort()));
			to.setUseHTTPS(String.valueOf(from.isUseHTTPS()));
			to.setResourcePrefix(from.getResourcePrefix());
			to.setSignatureVersion(String.valueOf(from.getSignatureVersion()));

			l.add(to);
		}
		
		setTestData(l.toArray(new ServerRecord[0]));
		setClientOnly(true);
	}
}
